package gsb.gsb;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import modele.DAO;
import modele.Medecin;

public class AutreActivity extends ListActivity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.lesmeds);
        Intent inter = getIntent();
        String dep = inter.getStringExtra("leDep");
        final List<Medecin> lesMeds = DAO.getLesMeds(dep);

        MedecinAdapter adapter = new MedecinAdapter(lesMeds, this);
        setListAdapter(adapter);

        Button send = (Button) findViewById(R.id.buttonAffi);

        send.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                EditText text = (EditText) findViewById(R.id.text);
                List<Medecin> lesMeds2 = new ArrayList<Medecin>();
                for (Medecin m : lesMeds) {
                    if (m.getNom().toUpperCase().startsWith(text.getText().toString().toUpperCase())) {
                        lesMeds2.add(m);
                    }
                }
                MedecinAdapter adapter = new MedecinAdapter(lesMeds2, getApplicationContext());
                setListAdapter(adapter);
            }
        });
    }

    @Override
    public void onListItemClick(final ListView l, View v, final int position, long id) {
        AlertDialog popup = new AlertDialog.Builder(this).create();
        
        Medecin m = (Medecin) l.getItemAtPosition(position);
        String nom = m.getNom();
        String prenom = m.getPrenom();
        
        popup.setTitle(prenom + " " + nom);
        popup.setMessage("Que voulez-vous faire ?");

        final String contact = m.getTel();  
        final String adresse = m.getAdresse().replace(" ", "+");
     
        popup.setButton("Appel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:" + contact));
                startActivity(callIntent);
            }
        });
        popup.setButton2("SMS", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                sendIntent.setData(Uri.parse("sms:" + contact));
                startActivity(sendIntent);
            }
        });
        popup.setButton3("Plan", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Intent inter = new Intent(getApplicationContext(), MapActivity.class);
                inter.putExtra("adresse", adresse);
                startActivity(inter);
            }
        });

        popup.show();
    }
}
